import RPi.GPIO as GPIO
import time
last_pwm=7.5
def send_pwm(p_gpio,pwm_width):
    global last_pwm
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(p_gpio,GPIO.OUT,initial=GPIO.LOW)
    pwm=GPIO.PWM(p_gpio,50)
    pwm.start(0)
    time_start=time.time()
    if last_pwm<=pwm_width:
        while last_pwm<=pwm_width:
            pwm_current=float(format(last_pwm,'.3f'))
            pwm.ChangeDutyCycle(pwm_current)
            print('pwm',pwm_current)
            last_pwm=float(format(last_pwm+0.005,'.3f'))
            time.sleep(0.005)
        last_pwm=float(format(last_pwm-0.005,'.3f'))
    else:
        while last_pwm>=pwm_width:
            pwm_current=float(format(last_pwm,'.3f'))
            pwm.ChangeDutyCycle(pwm_current)
            print('pwm',pwm_current)
            last_pwm=float(format(last_pwm-0.005,'.3f'))
            time.sleep(0.005)
        last_pwm=float(format(last_pwm+0.005,'.3f'))
    time.sleep(0.02)
    pwm.stop()
    GPIO.cleanup()
    time_end=time.time()
    # last_pwm=float(format(last_pwm,'.3f'))
    print('time=',last_pwm,time_end-time_start)
    return
def main():
    global last_pwm
    notice="please input a number,[2.5,12.5] \n"
    s=input(notice)
    print('input',s,last_pwm)
    try:
        while True:
            send_pwm(7,s)
            time.sleep(1)
            s=input(notice)
            print('input',s,last_pwm)
    except Exception as e:
        pass
    return
main()